<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
            margin:0;
            font-family:"微软雅黑";
        }
        #container{
            width:100%;
            height:100%;
        }
        #result{
            position: fixed;
            top: 10px;
            left: 20px;
            width: 370px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background: #fff;
            box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
            border-radius: 7px;
            z-index: 99;
            color: #bd2c00;
        }
    </style>
    <script type="text/javascript" src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=34wG1jfGGOZ0ZWrS9jga0XY7mkBOGAb6"></script>
    <title>急救车辆调度系统</title>
</head>
<body>
<div id="container"></div>
<div id='result'>根据起点和终点经纬度驾车导航路线</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var map = new BMapGL.Map("container");

    var lat = 119.18910
    var lng = 34.61238

    map.centerAndZoom(new BMapGL.Point(lat, lng), 11);
    map.enableScrollWheelZoom(true);

    var marker1 = new BMapGL.Marker(new BMapGL.Point(lat+0.01, lng-0.01));
    var marker2 = new BMapGL.Marker(new BMapGL.Point(lat+0.005, lng-0.011));
    var marker3 = new BMapGL.Marker(new BMapGL.Point(lat-0.015, lng-0.012));
    var marker5 = new BMapGL.Marker(new BMapGL.Point(lat+0.031, lng+0.04));
    var marker4 = new BMapGL.Marker(new BMapGL.Point(lat-0.009, lng+0.017));

    map.addOverlay(marker1);
    map.addOverlay(marker2);
    map.addOverlay(marker3);
    map.addOverlay(marker4);
    map.addOverlay(marker5);

    function sleep(delay) {
        var start = (new Date()).getTime();
        while((new Date()).getTime() - start < delay) {
            continue;
        }
    }

    map.addEventListener('click', function(e){
        //清除地图上所有的覆盖物
        var pt = e.latlng;

        var marker = new BMapGL.Marker(new BMapGL.Point(pt.lng, pt.lat));
        map.addOverlay(marker);

        var geoc = new BMapGL.Geocoder();

        geoc.getLocation(pt, function(rs){

            r_lat = pt.lng
            r_lng = pt.lat

            var p1 = new BMapGL.Point(r_lat, r_lng);
            var p2 = new BMapGL.Point(lat, lng);

            var addComp = rs.addressComponents;

            var ele=document.getElementById("result")
            ele.style.height = "130px"
            var place = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
            var hospital = "连云港市第一人民医院"
            var driver = "韩国凯"
            var phone = ""
            var doctor = "王家龙"
            var nourse = "朱甜"
            var car = "苏G06896"
            var id = "101"
            var info ="求助位置:" + place + "、\n病人联系方式：" + phone
            var info_temp ="求助位置:" + place + "<br/> " + "病人联系方式号码：" + phone
            var mark = "暂无"

            sleep(800)

            ele.innerText = info
            $.modal.confirm("确认是否添加此条救护信息?<br/>"+info_temp, function() {
                send()
            });

            function send(){
                $.ajax({
                    url: '/am/route/add',
                    type: 'post',
                    data: {
                        "hosLatLng":lat+";"+lng,
                        "hosName":hospital,
                        "patLatLng":r_lat+";"+r_lng,
                        "patPlace":place,
                        "driverName":driver,
                        "patMark":mark,
                        "patPhone":phone,
                        "doctor":doctor,
                        "nourse":nourse,
                        "carNum":car,
                        "driverId":id,
                        "status":"0"
                    },
                    success: function (data) {
                        if (data.code === 0){
                            $.modal.alertSuccess("添加成功，任务已分配！");
                            var driving = new BMapGL.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}});
                            driving.search(p1, p2);
                        }
                        else {
                            $.modal.alertError(data.msg);
                        }
                        console.log(data)
                    },
                    error: function (json) {
                        $.modal.alertError("系统出现错误，请联系管理员！");
                        console.log(json.status);
                    }
                });
            }
        })
    })
</script>
</body>
</html>
